package com.zero.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.logistics.pojo.MessageObject;
import com.zero.logistics.pojo.Role;
import com.zero.logistics.pojo.RoleExample;
import com.zero.logistics.pojo.RoleExample.Criteria;
import com.zero.logistics.pojo.User;
import com.zero.logistics.pojo.UserExample;
import com.zero.logistics.service.RoleService;
import com.zero.logistics.service.UserService;

/*
 *	@date:2020年3月31日 上午5:00:13
 *	@author:Zero
 *	@version:Future
 *	@description: 
 *
 */
@Controller
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;

	@RequiresPermissions("role:rolePage")
	@RequestMapping("/rolePage")
	public String rolePage() {
		return "rolePage";
	}
	
	/**
	 * @function 分页显示角色
	 * @param pageNum
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@RequiresPermissions("role:list")
	@RequestMapping("list")
	@ResponseBody  //返回json类型的数据
	public PageInfo<Role> roleList(@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "10")int pageSize,
			String keywords){
		
		PageHelper.startPage(pageNum, pageSize);
		RoleExample example = new RoleExample();
		
		// 定义搜索的逻辑（使用管理）
		if (StringUtils.isNotBlank(keywords)) {  //当前台传过来的关键字不等于空的时候
			//根据角色名查询
			Criteria criteria1 = example.createCriteria();
			criteria1.andRolenameLike("%" + keywords + "%");
			
		}
		
		List<Role> roles = roleService.selectByExample(example);
		PageInfo<Role> pageInfo = new PageInfo<>(roles);
		
		return pageInfo;
	}
	
	/**
	 * @function 删除角色
	 * @param roleId
	 * @return
	 */
	@RequiresPermissions("role:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject roleDelete(Long roleId) {
		//1.删除角色之前要判断当前是否还拥有属于该角色的用户
		UserExample example = new UserExample();
		com.zero.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<User> users = userService.selectByExample(example);
		if (users.size() > 0) {  //如果有，则不能删除
			return MessageObject.createMo(0, "此角色还关联有用户，不能删除");
		}
		//2.根据角色id删除角色
		int row = roleService.deleteByPrimaryKey(roleId);
		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 批量删除(以String类型接收前端传过来的参数)
	 * @param map
	 * @return
	 */
	@RequiresPermissions("role:batchdel")
	@RequestMapping("/batchdel")
	@ResponseBody
	public MessageObject roleBatchdel(String roleIds) {
		System.out.println(roleIds);
		String[] roleIdsArr = roleIds.split(",");
		
		//1.删除角色之前要判断当前是否还拥有属于该角色的用户
		for (String roleId : roleIdsArr) {
			UserExample example = new UserExample();
			com.zero.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
			criteria.andRoleIdEqualTo(Long.parseLong(roleId));
			List<User> users = userService.selectByExample(example);
			if (users.size() > 0) {  //如果有，则不能删除
				return MessageObject.createMo(0, "被删除的角色还关联有用户，不能删除");
			}
		}
		//2.根据角色id的数组批量删除角色
		int rows = roleService.deleteByIds(roleIdsArr);
		MessageObject mo = MessageObject.createMo(0, "批量删除数据失败，请联系技术人员");
		if (rows != 0) {
			mo = MessageObject.createMo(1, "批量删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 提供编辑页面(在新增角色和更新角色的时候都需要执行此方法)
	 * @param m
	 * @return
	 */
	@RequestMapping("/edit")
	public String roleEdit(Model m, Long roleId) {	
		//1.当前端传过来的roleId不为空的时候，就通过roleId查询角色信息并共享数据，用于更新的情况
		if (roleId != null) {
			Role role = roleService.selectByPrimaryKey(roleId);
			System.out.println("需要更新的角色信息：" + role.getRoleId());
			m.addAttribute("role", role);
		}

		return "roleEdit";
	}
	
	/**
	 * @function 判断角色名是否存在
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("role:checkRolename")
	@RequestMapping("/checkRolename")
	public boolean roleCheckName(String rolename) {
		RoleExample example = new RoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andRolenameEqualTo(rolename);
		List<Role> roles = roleService.selectByExample(example);
		return roles.size() == 0 ? true : false;
	}
	
	/**
	 * @function 新增角色
	 * @param role
	 * @return
	 */
	@RequiresPermissions("role:insert")
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject roleInsert(Role role) {
		// 1.插入数据并返回结果
		int row = roleService.insert(role);
		MessageObject mo = MessageObject.createMo(0, "添加数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "添加数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 更新角色信息
	 * @param role
	 * @return
	 */
	@RequiresPermissions("role:update")
	@RequestMapping("update")
	@ResponseBody
	public MessageObject roleUpdate(Role role) {
		// 1.更新数据并返回结果(这里要用动态sql语句)
		int row = roleService.updateByPrimaryKeySelective(role);
		MessageObject mo = MessageObject.createMo(0, "更新数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "更新数据成功");
		}
		return mo;
	}

}
